package com.grt.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.grt.admin.AdminConstants;
import com.grt.admin.dao.ArticleDao;
import com.grt.admin.dao.ArticleTypeDao;
import com.grt.admin.domain.Article;
import com.grt.admin.domain.ArticleType;
import com.grt.admin.service.ArticleService;
import com.grt.admin.util.ArticleQuery;
import com.grt.admin.util.ArticleTypeQuery;
import com.grt.common.util.StringUtil;

@Service
public class ArticleServiceImpl implements ArticleService {

	@Autowired
	private ArticleDao articleDao;

	@Autowired
	private ArticleTypeDao articleTypeDao;

	public Article saveArticle(Article oArticle) {
		Article article = articleDao.save(oArticle);
		return article;
	}

	/**
	 * 获取案列列表
	 * 
	 * @param pageindex
	 * @param pagesize
	 * @return
	 */
	public Page<Article> getArticleList(int pageindex, int pagesize) {
		Pageable pageable = new PageRequest(pageindex, pagesize, Sort.Direction.ASC, "id");
		Page<Article> page = articleDao.findAll(pageable);

		return page;
	}

	/**
	 * 获取首页案例列表
	 * 
	 * @return
	 */
	public List<Article> getIndexArticleList(ArticleQuery articleQuery) {
		int pagesize = articleQuery.getLimit() == 0 ? AdminConstants.DEF_PAGESIZE : articleQuery.getLimit();
		Pageable pageable = new PageRequest(AdminConstants.DEF_PAGEINDEX, pagesize);
		Page<Article> page = articleDao.findAll(new Specification<Article>() {
			@Override
			public Predicate toPredicate(Root<Article> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> prList = new ArrayList<Predicate>();
				String title = articleQuery.getTitle();
				if (!StringUtil.isEmpty(title)) {
					prList.add(criteriaBuilder.like(root.get("title").as(String.class), "%" + title + "%"));
				}
				String desc = articleQuery.getDesc();
				if (!StringUtil.isEmpty(desc)) {
					prList.add(criteriaBuilder.like(root.get("description").as(String.class), "%" + desc + "%"));
				}
				Long type = articleQuery.getType();
				if (type != null) {
					prList.add(criteriaBuilder.equal(root.get("typeid").as(Long.class), type));
				}

				String keywords = articleQuery.getKeywords();
				if (!StringUtil.isEmpty(keywords)) {
					prList.add(criteriaBuilder.like(root.get("keywords").as(String.class), "%" + keywords + "%"));
				}
				String author = articleQuery.getAuthor();
				if (!StringUtil.isEmpty(author)) {
					prList.add(criteriaBuilder.like(root.get("author").as(String.class), "%" + author + "%"));
				}

				Predicate[] p = new Predicate[prList.size()];
				return criteriaBuilder.and(prList.toArray(p));
			}
		}, pageable);
		return page.getContent();
	}

	public Article getArticleById(long articleId) {
		Article oArticle = articleDao.findOne(articleId);
		return oArticle;
	}

	public void deleteArticle(Article oArticle) {
		articleDao.delete(oArticle);
	}

	@Override
	public ArticleType saveArticleType(ArticleType oArticleType) {
		ArticleType articleType = articleTypeDao.save(oArticleType);
		return articleType;
	}

	@Override
	public Page<ArticleType> getArticleTypeList(int pageindex, int pagesize) {
		Pageable pageable = new PageRequest(pageindex, pagesize, Sort.Direction.ASC, "id");
		Page<ArticleType> page = articleTypeDao.findAll(pageable);

		return page;
	}

	@Override
	public List<ArticleType> getArticleTypeList(ArticleTypeQuery articleTypeQuery) {
		int pagesize = articleTypeQuery.getLimit() == null ? AdminConstants.DEF_PAGESIZE : articleTypeQuery.getLimit();
		Pageable pageable = new PageRequest(AdminConstants.DEF_PAGEINDEX, pagesize);
		Page<ArticleType> page = articleTypeDao.findAll(new Specification<ArticleType>() {
			@Override
			public Predicate toPredicate(Root<ArticleType> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> prList = new ArrayList<Predicate>();
				String typename = articleTypeQuery.getTypename();
				if (!StringUtil.isEmpty(typename)) {
					prList.add(criteriaBuilder.like(root.get("typename").as(String.class), "%" + typename + "%"));
				}
				String desc = articleTypeQuery.getTypedesc();
				if (!StringUtil.isEmpty(desc)) {
					prList.add(criteriaBuilder.like(root.get("typedesc").as(String.class), "%" + desc + "%"));
				}
				Integer state = articleTypeQuery.getState();
				if (state != null) {
					prList.add(criteriaBuilder.equal(root.get("state").as(Long.class), state));
				}

				Predicate[] p = new Predicate[prList.size()];
				return criteriaBuilder.and(prList.toArray(p));
			}
		}, pageable);
		return page.getContent();
	}

	@Override
	public ArticleType getArticleTypeById(long id) {
		ArticleType articleType = articleTypeDao.findOne(id);
		return articleType;
	}

}